package com.eric.windows.util;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;

import java.io.File;
import java.util.LinkedList;

/**
 * @author luzhenbang
 * @Description TODO 文件操作类
 * @date 2015/12/14
 * @Copyright: Copyright (c) 2015 Shenzhen Tentinet Technology Co., Ltd. Inc. All rights reserved.
 */
public class FileUtil {
    /**
     * 获取当前app的应用名字
     *
     * <h3>Version</h3> 1.0
     * <h3>CreateTime</h3> 2016/3/21,19:28
     * <h3>UpdateTime</h3> 2016/3/21,19:28
     * <h3>CreateAuthor</h3> luzhenbang
     * <h3>UpdateAuthor</h3>
     * <h3>UpdateInfo</h3> (此处输入修改内容,若无修改可不写.)
     *
     */
    public static String getAppName(Context context) {
        PackageManager packageManager = null;
        ApplicationInfo applicationInfo = null;
        try {
            packageManager = context.getApplicationContext().getPackageManager();
            applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            applicationInfo = null;
        }
        String applicationName = (String) packageManager.getApplicationLabel(applicationInfo);
        return applicationName;
    }

    /**
     *   App包名
     * <h3>Version</h3> 1.0
     * <h3>CreateTime</h3> 2016/3/21,22:27
     * <h3>UpdateTime</h3> 2016/3/21,22:27
     * <h3>CreateAuthor</h3> luzhenbang
     * <h3>UpdateAuthor</h3>
     * <h3>UpdateInfo</h3> (此处输入修改内容,若无修改可不写.)
     * @param context 上下文
     * @return App包名字符串 例如：com.tentinet.project
     */
    public static String getPackageName(Context context){
        String packageString = context.getPackageName();
        return packageString;
    }



    /**
     *   获取APP缓存路径
     * <h3>Version</h3> 1.0
     * <h3>CreateTime</h3> 2016/3/21,22:50
     * <h3>UpdateTime</h3> 2016/3/21,22:50
     * <h3>CreateAuthor</h3> luzhenbang
     * <h3>UpdateAuthor</h3>
     * <h3>UpdateInfo</h3> (此处输入修改内容,若无修改可不写.)
     * @param context 上下文
     * @return APP路径字符串
     */
    public static String getAppCachePath(Context context){
        String appPath = null;
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
             appPath = Environment.getExternalStorageState() + File.separator + getAppName(context);
        }else {
             appPath = Environment.getDataDirectory() + File.separator + getPackageName(context);
        }
        File file = new File(appPath);
        if (!file.exists()){
            file.mkdirs();
        }
        return appPath;
    }


    /**
     *   获取APP缓存文件
     * <h3>Version</h3> 1.0
     * <h3>CreateTime</h3> 2016/3/21,22:50
     * <h3>UpdateTime</h3> 2016/3/21,22:50
     * <h3>CreateAuthor</h3> luzhenbang
     * <h3>UpdateAuthor</h3>
     * <h3>UpdateInfo</h3> (此处输入修改内容,若无修改可不写.)
     * @param context 上下文
     * @return APP路径字符串
     */
    public static File getAppCacheAbsoluteFile(Context context){
        String appPath = null;
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            appPath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + getAppName(context) + File.separator;
        }else {
            appPath = Environment.getDataDirectory().getAbsolutePath() + File.separator + getPackageName(context) + File.separator;
        }
        File file = new File(appPath);
        if (!file.exists()){
            file.mkdirs();
        }
        return file;
    }


    /**
     * 查询媒体文件所在路径
     *
     * @version 1.0
     * @createTime 2015年7月9日,下午2:18:31
     * @updateTime 2015年7月9日,下午2:18:31
     * @createAuthor gaowei
     * @updateAuthor gaowei
     * @updateInfo
     * @param context
     * @param uri
     * @param selection
     * @param selectionArgs
     * @return
     */
    private static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
        Cursor cursor = null;
        String column = MediaStore.Images.Media.DATA;
        String[] projection = { column };
        try {
            cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
            if (cursor != null && cursor.moveToFirst()) {
                int index = cursor.getColumnIndexOrThrow(column);
                return cursor.getString(index);
            }
        } finally {
            if (cursor != null)
                cursor.close();
        }
        return null;
    }


    /**
     * 遍历指定文件夹根目录下的文件
     *
     * @version 1.0
     *
     * @createTime 2015/12/14,10:09
     * @updateTime 2015/12/14,10:09
     * @createAuthor luzhenbang
     * @updateAuthor
     * @updateInfo (此处输入修改内容,若无修改可不写.)
     * @param file 需要遍历的文件夹
     * @param list  存放文件链接的列表
     */
    private static float getDirRootSize(File file, LinkedList list) {
        // 每个文件夹遍历都会调用该方法
        float size = 0;
        File[] files = file.listFiles();
        if (files == null || files.length == 0) {
            return size;
        }

        for (File f : files) {
            if (f.isDirectory()) {
                list.add(f);
            } else {
                // 这里列出当前文件夹根目录下的所有文件
                size += f.length();
            }
        }
        return size;
    }


    /**
     * @param uri
     *        The Uri to check.
     * @return Whether the Uri authority is ExternalStorageProvider.
     */
    private static boolean isExternalStorageDocument(Uri uri) {
        return "com.android.externalstorage.documents".equals(uri.getAuthority());
    }

    /**
     * @param uri
     *        The Uri to check.
     * @return Whether the Uri authority is DownloadsProvider.
     */
    private static boolean isDownloadsDocument(Uri uri) {
        return "com.android.providers.downloads.documents".equals(uri.getAuthority());
    }

    /**
     * @param uri
     *        The Uri to check.
     * @return Whether the Uri authority is MediaProvider.
     */
    private static boolean isMediaDocument(Uri uri) {
        return "com.android.providers.media.documents".equals(uri.getAuthority());
    }

    /**
     * @param uri
     *        The Uri to check.
     * @return Whether the Uri authority is Google Photos.
     */
    private static boolean isGooglePhotosUri(Uri uri) {
        return "com.google.android.apps.photos.content".equals(uri.getAuthority());
    }

}
